﻿using Halcon二次开发.Controller.数据加载与保存;
using Halcon二次开发.Controller.膨胀或腐蚀;
using Halcon二次开发.Model;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.View.膨胀与腐蚀
{
    public partial class SwellEtchToolFrom : Form
    {
        public SwellEtchController controller = null;
        public SwellEtchToolFrom()
        {
            InitializeComponent();
            controller = new SwellEtchController($"..\\..\\Data\\工程文件\\{StaicResouce.HwindowFlowNodeUrse.name}工程文件\\图像处理校验数据",this);
        }

        private void SwellEtchToolFrom_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
            //保存模板数据
            controller.SetModelPaths();
        }
        /// <summary>
        /// 反射调用的方法，请不要修改
        /// </summary>
        /// <param name="path">Json文件的路径</param>
        /// <param name="addres">节点</param>
        public void SetJsonPaths(string path, string addres)
        {
            string paths = path + "\\" + "界面数据\\" + addres + "\\" + addres + "数据.json";
            //调用存储方法
            new LoadData().LoadDatas(paths, this);
            SwellEtchLoad();
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Run();
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }

        }
        
        private void SwellEtchLoad()
        {
            uiTrackBar1.Value = int.Parse(uiTrackBar1.Text);
            uiTrackBar2.Value = int.Parse(uiTrackBar2.Text);
            uiTrackBar3.Value = int.Parse(uiTrackBar3.Text);
            uiTrackBar4.Value = int.Parse(uiTrackBar4.Text);
            uiTrackBar5.Value = int.Parse(uiTrackBar5.Text);
            uiTrackBar6.Value = int.Parse(uiTrackBar6.Text);
            uiTrackBar7.Value = int.Parse(uiTrackBar7.Text);
            uiTrackBar8.Value = int.Parse(uiTrackBar8.Text);
            uiTrackBar9.Value = int.Parse(uiTrackBar9.Text);
            uiTrackBar10.Value = int.Parse(uiTrackBar10.Text);
            uiTrackBar11.Value = int.Parse(uiTrackBar11.Text);
            uiTrackBar12.Value = int.Parse(uiTrackBar12.Text);
            uiTrackBar13.Value = int.Parse(uiTrackBar13.Text);
            uiTrackBar14.Value = int.Parse(uiTrackBar14.Text);


            labValue1.Text = uiTrackBar1.Value.ToString();
            labValue2.Text = uiTrackBar2.Value.ToString();
            labValue3.Text = uiTrackBar3.Value.ToString();
            labValue4.Text = uiTrackBar4.Value.ToString();
            labValue5.Text = uiTrackBar5.Value.ToString();
            labValue6.Text = uiTrackBar6.Value.ToString();
            labValue7.Text = uiTrackBar7.Value.ToString();
            labValue8.Text = uiTrackBar8.Value.ToString();
            labValue9.Text = uiTrackBar9.Value.ToString();
            labValue10.Text = uiTrackBar10.Value.ToString();
            labValue11.Text = uiTrackBar11.Value.ToString();
            labValue12.Text = uiTrackBar12.Value.ToString();
            labValue13.Text = uiTrackBar13.Value.ToString();
            labValue14.Text = uiTrackBar14.Value.ToString();
            List<int> list= controller.parameter;
            tabControl1.SelectedIndex = list[0];
            if (list[1] == 1)
            {
                if (list[0] == 1)
                {
                    groupBox2.Enabled = true;
                    groupBox1.Enabled = false;
                }else if (list[0] == 2)
                {
                    groupBox7.Enabled = true;
                    groupBox8.Enabled = false;
                }
                else if (list[0] == 3)
                {
                    groupBox6.Enabled = true;
                    groupBox9.Enabled = false;
                }
                else if (list[0] == 4)
                {
                    groupBox11.Enabled = true;
                    groupBox12.Enabled = false;
                }
            }

            
        }
        /// <summary>
        /// 膨胀-矩形设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                groupBox1.Enabled = true;
                groupBox2.Enabled = false;
            }

            controller.SetParameter(tabControl1.SelectedIndex, radioButton2.Checked);
        }
        /// <summary>
        /// 膨胀-圆形设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                groupBox1.Enabled = false;
                groupBox2.Enabled = true;
            }
            controller.SetParameter(tabControl1.SelectedIndex, radioButton2.Checked);
        }
        /// <summary>
        /// 腐蚀-矩形设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton3_CheckedChanged_1(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                groupBox8.Enabled = true;
                groupBox7.Enabled = false;
            }
            controller.SetParameter(tabControl1.SelectedIndex, radioButton3.Checked);
        }
        /// <summary>
        /// 腐蚀-圆形设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton4_CheckedChanged_1(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                groupBox8.Enabled = false;
                groupBox7.Enabled = true;
            }
            controller.SetParameter(tabControl1.SelectedIndex, radioButton3.Checked);
        }
        /// <summary>
        /// 开运算-矩形设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton5_CheckedChanged_1(object sender, EventArgs e)
        {
            if (radioButton5.Checked)
            {
                groupBox9.Enabled = true;
                groupBox6.Enabled = false;
            }
            controller.SetParameter(tabControl1.SelectedIndex, radioButton5.Checked);
        }
        /// <summary>
        /// 开运算-圆形设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton6_CheckedChanged_1(object sender, EventArgs e)
        {
            if (radioButton6.Checked)
            {
                groupBox9.Enabled = false;
                groupBox6.Enabled = true;
            }
            controller.SetParameter(tabControl1.SelectedIndex, radioButton5.Checked);
        }
        /// <summary>
        /// 闭运算-矩形设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton7_CheckedChanged_1(object sender, EventArgs e)
        {
            if (radioButton7.Checked)
            {
                groupBox12.Enabled = true;
                groupBox11.Enabled = false;
            }
            controller.SetParameter(tabControl1.SelectedIndex, radioButton7.Checked);
        }
        /// <summary>
        /// 闭运算-圆形设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton8_CheckedChanged_1(object sender, EventArgs e)
        {
            if (radioButton8.Checked)
            {
                groupBox12.Enabled = false;
                groupBox11.Enabled = true;
            }
            controller.SetParameter(tabControl1.SelectedIndex, radioButton7.Checked);
        }
       
        #region 控件value改变时
        /// <summary>
        /// 二值化，取值下限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar1_ValueChanged(object sender, EventArgs e)
        {
            labValue1.Text = uiTrackBar1.Value.ToString();
            uiTrackBar1.Text = uiTrackBar1.Value.ToString();
        }
        /// <summary>
        /// 二值化，取值上限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar2_ValueChanged(object sender, EventArgs e)
        {
            labValue2.Text = uiTrackBar2.Value.ToString();
            uiTrackBar2.Text = uiTrackBar2.Value.ToString();
        }
        /// <summary>
        /// 膨胀-宽度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar3_ValueChanged(object sender, EventArgs e)
        {
            labValue3.Text = uiTrackBar3.Value.ToString();
            uiTrackBar3.Text = uiTrackBar3.Value.ToString();
        }
        /// <summary>
        /// 膨胀-高度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar4_ValueChanged(object sender, EventArgs e)
        {
            labValue4.Text = uiTrackBar4.Value.ToString();
            uiTrackBar4.Text = uiTrackBar4.Value.ToString();
        }
        /// <summary>
        /// 膨胀-半径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar5_ValueChanged(object sender, EventArgs e)
        {
            labValue5.Text = uiTrackBar5.Value.ToString();
            uiTrackBar5.Text = uiTrackBar5.Value.ToString();
        }
        /// <summary>
        /// 腐蚀-宽度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar7_ValueChanged(object sender, EventArgs e)
        {
            labValue7.Text = uiTrackBar7.Value.ToString();
            uiTrackBar7.Text = uiTrackBar7.Value.ToString();
        }
        /// <summary>
        /// 腐蚀-高度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar8_ValueChanged(object sender, EventArgs e)
        {
            labValue8.Text = uiTrackBar8.Value.ToString();
            uiTrackBar8.Text = uiTrackBar8.Value.ToString();
        }
        /// <summary>
        /// 腐蚀-半径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar6_ValueChanged(object sender, EventArgs e)
        {
            labValue6.Text = uiTrackBar6.Value.ToString();
            uiTrackBar6.Text = uiTrackBar6.Value.ToString();
        }
        /// <summary>
        /// 开运算-宽度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar10_ValueChanged(object sender, EventArgs e)
        {
            labValue10.Text = uiTrackBar10.Value.ToString();
            uiTrackBar10.Text = uiTrackBar10.Value.ToString();
        }
        /// <summary>
        /// 开运算-高度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar11_ValueChanged(object sender, EventArgs e)
        {
            labValue11.Text = uiTrackBar11.Value.ToString();
            uiTrackBar11.Text = uiTrackBar11.Value.ToString();
        }
        /// <summary>
        /// 开运算-半径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar9_ValueChanged(object sender, EventArgs e)
        {
            labValue9.Text = uiTrackBar9.Value.ToString();
            uiTrackBar9.Text = uiTrackBar9.Value.ToString();
        }
        /// <summary>
        /// 闭运算-宽度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar13_ValueChanged(object sender, EventArgs e)
        {
            labValue13.Text = uiTrackBar13.Value.ToString();
            uiTrackBar13.Text = uiTrackBar13.Value.ToString();
        }
        /// <summary>
        /// 闭运算-高度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar14_ValueChanged(object sender, EventArgs e)
        {
            labValue14.Text = uiTrackBar14.Value.ToString();
            uiTrackBar14.Text = uiTrackBar14.Value.ToString();
        }
        /// <summary>
        /// 闭运算-半径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTrackBar12_ValueChanged(object sender, EventArgs e)
        {
            labValue12.Text = uiTrackBar12.Value.ToString();
            uiTrackBar12.Text = uiTrackBar12.Value.ToString();
        }
        #endregion

        /// <summary>
        /// 切换选项卡时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == -1) return;
            bool value2 = false;
            if (tabControl1.SelectedIndex == 1)//膨胀
            {
                value2 = radioButton2.Checked;
            }
            else if (tabControl1.SelectedIndex == 2)//腐蚀
            {
                value2 = radioButton3.Checked;
            }
            else if (tabControl1.SelectedIndex == 3)//开运算
            {
                value2 = radioButton5.Checked;
            }
            else if (tabControl1.SelectedIndex == 4)//闭运算
            {
                value2 = radioButton7.Checked;
            }
            controller.SetParameter(tabControl1.SelectedIndex, value2);
        }
    }
}
